package com.tanhua.server.controller;

import com.tanhua.server.service.SettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;

    /**
     * 读取用户的通用设置
     * 请求连接：
     *  GET  /settings
     */
    @GetMapping("/settings")
    public ResponseEntity querySettings() {
        return settingsService.querySettings();
    }

    /**
     * 通知设置
     *   POST /notifications/setting
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity updateNotification(@RequestBody Map map) {
        //1、获取输入内容
        boolean like = (Boolean) map.get("likeNotification");
        boolean pinglun = (Boolean) map.get("pinglunNotification");
        boolean gonggao = (Boolean) map.get("gonggaoNotification");
        //2、调用service保存或者更新
        return settingsService.updateNotification(like,pinglun,gonggao);
    }

    /**
     * 设置陌生人问题
     *  请求连接
     *      POST  /questions
     *      参数： body ：content
     */
    @PostMapping("/questions")
    public ResponseEntity saveQuestions(@RequestBody Map map) {
        //1、获取输入内容
        String content = (String) map.get("content");
        //2、调用service保存或者更新
        return settingsService.saveQuestions(content);
    }


    /**
     * 分页查询黑名单列表
     *  请求连接：
     *      GET /blacklist
     *  请求参数：
     *      Query参数：
     *          page：当前页
     *          pagesize：每页查询条数
     */
    @GetMapping("/blacklist")
    public ResponseEntity findBlackList(@RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "10")  int pagesize) {
        return settingsService.findBlackList(page,pagesize);
    }

    /**
     * 移除黑名单
     *  请求连接：
     *      DELETE  /blacklist/{uid}
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity delBlacklist(@PathVariable("uid") long deleteUserId) {
        return settingsService.delBlacklist(deleteUserId);
    }

    /**
     * 修改手机号码：1.发送验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendValidateCode(){
        settingsService.sendValidateCode();
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号 - 2 校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkValidateCode(@RequestBody Map<String,String> param){
        boolean flag = settingsService.checkValidateCode(param.get("verificationCode"));
        Map<String,Boolean> result = new HashMap<String,Boolean>();
        result.put("verification",flag);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改手机号 - 3 保存
     */
    @PostMapping("/phone")
    public ResponseEntity changeMobile(@RequestBody Map<String,String> param, @RequestHeader("Authorization") String token){
        settingsService.changeMobile(param.get("phone"),token);
        return ResponseEntity.ok(null);
    }
}